import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '@/views/home/HomeView.vue'
import {ElMessage} from "element-plus";
import {delLocalStorage, delSessionStorage, getLocalStorage} from "@/util/storage.util";
import LoginView from "@/views/login/LoginView.vue";
import {verifyToken} from "@/service/token.service";
import TeacherExamInfoArrangementView from "@/views/teacher/exam/info/ArrangementView.vue";
import TeacherExamInfoMakeupArrangementView from "@/views/teacher/exam/info/MakeupArrangementView.vue";
import TeacherExamInfoRetakeArrangementView from "@/views/teacher/exam/info/RetakeArrangementView.vue";
import TeacherExamInfoDataStatisticsView from "@/views/teacher/exam/info/DataStatisticsView.vue";
import TeacherExamAssessmentMethodView from "@/views/teacher/exam/AssessmentMethodView.vue";
import TeacherExamCourseSchedulingView from "@/views/teacher/exam/CourseSchedulingView.vue";
import TeacherMaterialSubscriptionPlanView from "@/views/teacher/material/SubscriptionPlanView.vue";
import TeacherMaterialSelectionExchangeView from "@/views/teacher/material/SelectionExchangeView.vue";
import TeacherMaterialTeachingAidsView from "@/views/teacher/material/TeachingAidsView.vue";
import TeacherMaterialNewInventoryView from "@/views/teacher/material/NewInventoryView.vue";
import TeacherMaterialDistributionView from "@/views/teacher/material/DistributionView.vue";
import TeacherMaterialDataStatisticsView from "@/views/teacher/material/DataStatisticsView.vue";
import TeacherClassroomApplicationView from "@/views/teacher/classroom/ApplicationView.vue";
import TeacherClassroomMyApplicationView from "@/views/teacher/classroom/MyApplicationView.vue";
import TeacherClassroomApplicationManagementView from "@/views/teacher/classroom/ApplicationManagementView.vue";
import TeacherCourseChangeApplicationView from "@/views/teacher/course-change/ApplicationView.vue";
import TeacherCourseChangeApplicationManagementView from "@/views/teacher/course-change/ApplicationManagementView.vue";
import GraduationInfoManagementView from "@/views/teacher/graduation/GraduationInfoManagementView.vue";
import AttendanceView from "@/views/teacher/assessment/AttendanceView.vue";
import LogView from "@/views/teacher/assessment/LogView.vue";
import TeacherTeachingDataTalentPlanView from "@/views/teacher/teaching-data/TalentPlanView.vue";
import TeacherTeachingDataCourseInfoView from "@/views/teacher/teaching-data/CourseInfoView.vue";
import TeacherTeachingDataMaterialsCourseStandardView
    from "@/views/teacher/teaching-data/materials/CourseStandardView.vue";
import TeacherTeachingDataMaterialsTeachingPlanView from '@/views/teacher/teaching-data/materials/TeachingPlanView.vue';
import TeacherTeachingDataMaterialsCoursewareManagementView
    from "@/views/teacher/teaching-data/materials/CoursewareManagementView.vue";
import TeacherTeachingDataScheduleInfoView from '@/views/teacher/teaching-data/ScheduleInfoView.vue';
import TeacherTeachingDataTeacherInfoView from "@/views/teacher/teaching-data/TeacherInfoView.vue";
import StudentTeachingDataTalentPlanView from "@/views/student/teaching-data/TalentPlanView.vue";
import StudentExamMyExamsArrangementView from "@/views/student/exam/my-exams/ArrangementView.vue";
import StudentExamMyExamsReminderView from "@/views/student/exam/my-exams/ReminderView.vue";
import StudentExamAdmitCardView from "@/views/student/exam/AdmitCardView.vue";
import StudentExamResultsView from "@/views/student/exam/ResultsView.vue";
import StudentExamMakeupArrangementView from "@/views/student/exam/MakeupArrangementView.vue";
import StudentExamRetakeView from "@/views/student/exam/RetakeView.vue";
import StudentExamRetakeArrangementView from "@/views/student/exam/RetakeArrangementView.vue";
import StudentExamWarningReminderView from "@/views/student/exam/WarningReminderView.vue";
import StudentCourseChangeApplicationView from "@/views/student/course-change/ApplicationView.vue";
import StudentClassroomMyApplicationView from "@/views/student/classroom/MyApplicationView.vue";
import StudentClassroomApplicationView from '@/views/student/classroom/ApplicationView.vue';
import StudentGraduationView from "@/views/student/graduation/GraduationView.vue";
import StudentUpgradeView from "@/views/student/upgrade/UpgradeView.vue";
import StudentTeachingDataCourseInfoView from "@/views/student/teaching-data/CourseInfoView.vue";
import StudentTeachingDataMaterialsCourseStandardView
    from "@/views/student/teaching-data/materials/CourseStandardView.vue";
import StudentTeachingDataMaterialsTeachingPlanView from "@/views/student/teaching-data/materials/TeachingPlanView.vue";
import StudentTeachingDataMaterialsCoursewareView from "@/views/student/teaching-data/materials/CoursewareView.vue";
import StudentTeachingDataTeacherInfoView from "@/views/student/teaching-data/TeacherInfoView.vue";
import StudentTeachingDataScheduleInfoView from "@/views/student/teaching-data/ScheduleInfoView.vue";
import UserInfo from '@/views/users/user-info/UserInfo.vue';
import DataManagementView from "@/views/teacher/data-management/DataManagementView.vue";

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/users/user-info',
            name: 'UserInfo',
            component: UserInfo,
        },
        {
            path: '/',
            name: 'home',
            component: HomeView,
            children: [

                {
                    path: '/student/teaching-data/talent-plan',
                    name: 'student-teaching-data-talent-plan',
                    component: StudentTeachingDataTalentPlanView
                },
                {
                    path: '/student/exam/my-exams/arrangement',
                    name: 'student-exam-my-exams-arrangement',
                    component: StudentExamMyExamsArrangementView

                },
                {
                    path: '/student/exam/my-exams/reminder',
                    name: 'student-exam-my-exams-reminder',
                    component: StudentExamMyExamsReminderView
                },
                {
                    path: '/student/exam/admit-card',
                    name: 'student-exam-admit-card',
                    component: StudentExamAdmitCardView
                },
                {
                    path: '/student/exam/results',
                    name: 'student-exam-results',
                    component: StudentExamResultsView
                },
                {
                    path: '/student/exam/makeup-arrangement',
                    name: 'student-exam-makeup-arrangement',
                    component: StudentExamMakeupArrangementView
                },
                {
                    path: '/student/teaching-data/course-info',
                    name: 'student-teaching-data-course-info',
                    component: StudentTeachingDataCourseInfoView
                },
                {
                    path: '/teacher/teaching-data/talent-plan',
                    name: 'teacher-teaching-data-talent-plan',
                    component: TeacherTeachingDataTalentPlanView
                },
                {
                    path: '/teacher/teaching-data/course-info',
                    name: 'teacher-teaching-data-course-info',
                    component: TeacherTeachingDataCourseInfoView
                },
                {
                    path: '/teacher/teaching-data/materials/course-standard',
                    name: 'teacher-teaching-data-materials-course-standard',
                    component: TeacherTeachingDataMaterialsCourseStandardView
                },
                {
                    path: '/teacher/teaching-data/materials/teaching-plan',
                    name: 'teacher-teaching-data-materials-teaching-plan',
                    component: TeacherTeachingDataMaterialsTeachingPlanView
                },
                {
                    path: '/teacher/teaching-data/materials/courseware-management',
                    name: 'teacher-teaching-data-materials-courseware-management',
                    component: TeacherTeachingDataMaterialsCoursewareManagementView
                },
                {
                    path: '/teacher/teaching-data/teacher-info',
                    name: 'teacher-teaching-data-teacher-info',
                    component: TeacherTeachingDataTeacherInfoView
                },
                {
                    path: '/teacher/teaching-data/schedule-info',
                    name: 'teacher-teaching-data-schedule-info',
                    component: TeacherTeachingDataScheduleInfoView
                },
                {
                    path: '/student/exam/retake',
                    name: 'student-exam-retake',
                    component: StudentExamRetakeView
                },
                {
                    path: '/student/teaching-data/materials/course-standard',
                    name: 'student-teaching-data-materials-course-standard',
                    component: StudentTeachingDataMaterialsCourseStandardView
                },
                {
                    path: '/student/exam/retake-arrangement',
                    name: 'student-exam-retake-arrangement',
                    component: StudentExamRetakeArrangementView
                },
                {
                    path: '/teacher/exam/info/arrangement',
                    name: 'teacher-exam-info-arrangement',
                    component: TeacherExamInfoArrangementView
                },
                {
                    path: '/teacher/exam/info/makeup-arrangement',
                    name: 'teacher-exam-info-makeup-arrangement',
                    component: TeacherExamInfoMakeupArrangementView
                },
                {
                    path: '/teacher/exam/info/retake-arrangement',
                    name: 'teacher-exam-info-retake-arrangement',
                    component: TeacherExamInfoRetakeArrangementView
                },
                {
                    path: '/teacher/exam/info/data-statistics',
                    name: 'teacher-exam-info-data-statistics',
                    component: TeacherExamInfoDataStatisticsView
                },
                {
                    path: '/teacher/exam/assessment-method',
                    name: 'teacher-exam-assessment-method',
                    component: TeacherExamAssessmentMethodView
                },
                {
                    path: '/teacher/exam/course-scheduling',
                    name: 'teacher-exam-course-scheduling',
                    component: TeacherExamCourseSchedulingView
                },
                {
                    path: '/teacher/material/subscription-plan',
                    name: 'teacher-material-subscription-plan',
                    component: TeacherMaterialSubscriptionPlanView
                },
                {
                    path: '/teacher/material/selection-exchange',
                    name: 'teacher-material-selection-exchange',
                    component: TeacherMaterialSelectionExchangeView
                },
                {
                    path: '/teacher/material/teaching-aids',
                    name: 'teacher-material-teaching-aids',
                    component: TeacherMaterialTeachingAidsView
                },
                {
                    path: '/teacher/material/new-inventory',
                    name: 'teacher-material-new-inventory',
                    component: TeacherMaterialNewInventoryView
                },
                {
                    path: '/teacher/material/distribution',
                    name: 'teacher-material-distribution',
                    component: TeacherMaterialDistributionView
                },
                {
                    path: '/teacher/material/data-statistics',
                    name: 'teacher-material-data-statistics',
                    component: TeacherMaterialDataStatisticsView
                },
                {
                    path: '/teacher/classroom/application',
                    name: 'teacher-classroom-application',
                    component: TeacherClassroomApplicationView
                },
                {
                    path: '/teacher/classroom/my-application',
                    name: 'teacher-classroom-my-application',
                    component: TeacherClassroomMyApplicationView
                },
                {
                    path: '/teacher/classroom/application-management',
                    name: 'application-management',
                    component: TeacherClassroomApplicationManagementView
                },
                {
                    path: '/teacher/course-change/application',
                    name: 'teacher-course-change-application',
                    component: TeacherCourseChangeApplicationView
                },
                {
                    path: '/teacher/course-change/application-management',
                    name: 'teacher-course-change-application-management',
                    component: TeacherCourseChangeApplicationManagementView
                },
                {
                    path: '/teacher/graduate/info-management',
                    name: 'teacher/graduate/info-management',
                    component: GraduationInfoManagementView
                },
                {
                    path: '/teacher/assessment/attendance',
                    name: '/teacher/assessment/attendance',
                    component: AttendanceView
                },
                {
                    path: '/teacher/assessment/log',
                    name: '/teacher/assessment/log',
                    component: LogView
                },
                {
                    path: '/student/teaching-data/materials/teaching-plan',
                    name: 'student-teaching-data-materials-teaching-plan',
                    component: StudentTeachingDataMaterialsTeachingPlanView
                },
                {
                    path: '/student/exam/warning-reminder',
                    name: 'student-exam-warning-reminder',
                    component: StudentExamWarningReminderView
                },
                {
                    path: '/student/teaching-data/materials/courseware',
                    name: 'student-teaching-data-materials-courseware',
                    component: StudentTeachingDataMaterialsCoursewareView
                },
                {
                    path: '/student/classroom/application',
                    name: 'student-classroom-application',
                    component: StudentClassroomApplicationView
                },
                {
                    path: '/student/classroom/my-application',
                    name: 'student-classroom-my-application',
                    component: StudentClassroomMyApplicationView
                },
                {
                    path: '/student/teaching-data/teacher-info',
                    name: 'student-teaching-data-teacher-info',
                    component: StudentTeachingDataTeacherInfoView
                },
                {
                    path: '/student/teaching-data/schedule-info',
                    name: 'student-teaching-data-schedule-info',
                    component: StudentTeachingDataScheduleInfoView
                },
                {
                    path: '/student/course-change/application',
                    name: 'student-course-change-application',
                    component: StudentCourseChangeApplicationView
                },
                {
                    path: '/student/graduation',
                    name: 'student-graduation',
                    component: StudentGraduationView
                },
                {
                    path: '/student/upgrade',
                    name: 'student-upgrade',
                    component: StudentUpgradeView
                },
                {
                    path: '/teacher/data-management',
                    name: 'data-management',
                    component: DataManagementView
                },
            ]
        },
        {
            path: '/login',
            name: 'login',
            component: LoginView
        },

    ]
})

//全局路由守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        // 如果目标路由是 /login，则直接放行
        next()
    } else {
        // 否则进行身份验证、权限判断等操作
        //添加token
        let token = getLocalStorage("Token");
        //判断有没有token
        if (token) {
            console.log(token)
            if (token != '') {
                // 验证token有效
                verifyToken(token)
                    .then(value => {
                        if (value) {
                            console.log("Token有效")
                            next() // 放行
                        }
                    })
                    .catch(err => {
                        console.log(err)
                        console.log("Token无效")
                        try {
                            delLocalStorage("Token")
                            delSessionStorage("Token")
                        } finally {
                            next('/login') // 重定向到登录页面
                            ElMessage.error('请重新登录')
                        }
                    })
            }
        } else {
            next('/login') // 重定向到登录页面
            ElMessage.error('请先登录')
        }
    }
})


export default router
